'use strict';
const Service = require('egg').Service;

// Recycle封装
class RecycleService extends Service {
  // 查询双重签名是否使用
  async signHashStatusRecycle2(messageHash) {
    const { app, ctx, service } = this;
    const contract  = app.BSC.RecycleContract;
    // 返回true或false
    return await contract.methods.signHash(messageHash).call();
  }

  // 查询Recycle的orderId是否使用
  async orderIdMappingRecycle2(orderId) {
    const { app, ctx, service } = this;
    const contract  = app.BSC.RecycleContract;
    // 返回true或false
    return await contract.methods.orderIdMapping(orderId).call();
  }

  // 挖矿合约监听的封装
  async scanEventRecycle2(event_name, min_timestamp, max_timestamp) {
    const { app, ctx, service } = this;
    const tronGrid = app.BSC.TronGrid;
    const contractAddress = app.BSC.RecycleAddress;
    // 监听这个事件的时间区间之内的所有事件,但是不能超过200条
    const options1 = {
      event_name: event_name,
      only_confirmed: false,
      // block_number: '18124224',
      min_timestamp: min_timestamp,
      max_timestamp: max_timestamp,
      limit: 200,
    };
    const eventRes1 = await tronGrid.contract.getEvents(contractAddress, options1);
    return eventRes1.data;
  }

  // 用户提取收益加本金的二次签名;
  async withdrawRecycleSecondSignature2(
    orderId, userAddress0x, etmValue, nonce
  ) {
    const { app, ctx, service } = this;
    const privateKey = app.BSC.OwnerPrivateKey;
    const sha3Data = app.ETH.Web3.utils.soliditySha3(
      "withdraw", orderId, userAddress0x, etmValue, nonce
    );
    return app.ETH.Web3.eth.accounts.sign(sha3Data, privateKey);
  }

  // 用户领取挖矿收益的二次签名
  async userRedRecycleSecondSignature2(
    userAddress0x, etmValue, nonce
  ) {
    const { app, ctx, service } = this;
    const privateKey = app.BSC.OwnerPrivateKey;
    const sha3Data = app.ETH.Web3.utils.soliditySha3(
      "userRed", userAddress0x, etmValue, nonce
    );
    return app.ETH.Web3.eth.accounts.sign(sha3Data, privateKey);
  }

  // 回收NFT卡牌二次签名;
  async recycleCardSecondSignature2(
    nftId, userAddress0x, etmValue, nonce
  ) {
    const { app, ctx, service } = this;
    const privateKey = app.BSC.OwnerPrivateKey;
    const sha3Data = app.ETH.Web3.utils.soliditySha3(
      "recycle", nftId, userAddress0x, etmValue, nonce
    );
    return app.ETH.Web3.eth.accounts.sign(sha3Data, privateKey);
  }


}

module.exports = RecycleService;
